//
//  DropDownView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/16.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "DropDownView.h"
#import "GoodsTypeEntity.h"

@interface DropDownView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UITableView *tableView;


@end

@implementation DropDownView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)setupSubViews
{
    self.backgroundColor = [UIColor clearColor];
    self.contentView = [[UIView alloc] init];
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor blackColor];
    self.contentView.alpha = 0;
    [self addSubview:self.tableView];
    self.contentView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
    [self addGestureRecognizer:tap];
}

- (void)dismiss:(UITapGestureRecognizer *)tap
{
    self.hidden = YES;
}

- (void)setShowRect:(CGRect)showRect
{
    _showRect = showRect;
    self.contentView.frame = showRect;
    CGRect rect = self.tableView.frame;
    rect.origin.x = showRect.origin.x;
    rect.origin.y = showRect.origin.y;
    rect.size.width = showRect.size.width;
    self.tableView.frame = rect;
}

- (void)setGoodsTypeDataSource:(NSArray<GoodsTypeEntity *> *)goodsTypeDataSource
{
    _goodsTypeDataSource = goodsTypeDataSource;
    [self.tableView reloadData];
    self.tableView.height = 0;
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
        CGRect rect = self.tableView.frame;
        rect.size.height = MIN(44*self.goodsTypeDataSource.count, self.showRect.size.height);
        self.tableView.frame = rect;
        self.contentView.alpha = 0.5;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)setSelectIndexPath:(NSIndexPath *)selectIndexPath
{
    _selectIndexPath = selectIndexPath;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.goodsTypeDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"typeCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"typeCell"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    GoodsTypeEntity *type = self.goodsTypeDataSource[indexPath.row];
    cell.textLabel.text = type.name;
    if (self.selectIndexPath.row == indexPath.row && self.selectIndexPath) {
        cell.textLabel.textColor = UIColorFromRGB(0xF6A163);
    }
    else {
        cell.textLabel.textColor = kColor333333;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectIndexPath = indexPath;
    if (self.selectHandler) {
        self.selectHandler(indexPath);
        self.contentView.alpha = 0;
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.bounces = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
        _tableView.separatorColor = kColorDDD;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end
